package org.bizming.record.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.bizming.base.service.SqlHelper;
import org.bizming.record.model.RecordType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class RecordTypeDaoMysql implements RecordTypeDao {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private JdbcTemplate jdbcTemplate;

	private SqlHelper sqlHelper;

	private String tableName = "recordtype";

	public boolean add(RecordType recordType) {

		List<Object> values = new ArrayList<Object>();
		String sql = sqlHelper.sqlAdd(recordType, values, getTableName());
		logger.info(sql);

		Object[] o = values.toArray();
		int affectRow = jdbcTemplate.update(sql, o);

		sqlHelper.clear(values);
		sqlHelper.clear(o);

		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean update(RecordType recordType) {
		List<Object> values = new ArrayList<Object>();
		String sql = sqlHelper.sqlUpdate(recordType, values, getTableName());
		logger.info(sql);

		values.add(recordType.getId());
		Object[] o = values.toArray();
		int affectRow = jdbcTemplate.update(sql, o);

		sqlHelper.clear(values);
		sqlHelper.clear(o);
		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}
	}

	public RecordType get(int id) {

		String sql = sqlHelper.sqlGet(null, getTableName());
		logger.info(sql);

		List<RecordType> rows = jdbcTemplate.query(sql, new RecordTypeMapper(),
				id);
		if (rows != null && rows.size() > 0) {
			return rows.get(0);
		}

		return null;
	}

	public boolean remove(int id) {

		String sql = sqlHelper.sqlRemove(getTableName());
		logger.info(sql);

		int affectRow = jdbcTemplate.update(sql, id);
		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}

	}

	public List<RecordType> selectList(int limit, int page) {

		String sql = sqlHelper.sqlSelectList(RecordType.class, getTableName());
		logger.info(sql);

		Object[] args = new Object[] { limit, page };

		List<RecordType> rows = jdbcTemplate.query(sql, new RecordTypeMapper(),
				args);

		sqlHelper.clear(args);

		return rows;
	}

	class RecordTypeMapper implements RowMapper<RecordType> {

		public RecordType mapRow(ResultSet rs, int rowNum) throws SQLException {
			RecordType recordType = new RecordType();
			recordType.setId(rs.getInt("id"));
			recordType.setType(rs.getString("type"));
			recordType.setUsername(rs.getString("username"));
			recordType.setCreateTime(rs.getLong("createTime"));
			recordType.setActionTime(rs.getLong("actionTime"));
			recordType.setDescription(rs.getString("description"));
			return recordType;
		}

	}

	public int count() {
		String sql = sqlHelper.sqlCount(getTableName());
		logger.info(sql);

		int row = jdbcTemplate.queryForInt(sql);
		return row;
	}

	// ====================================
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public SqlHelper getSqlHelper() {
		return sqlHelper;
	}

	public void setSqlHelper(SqlHelper sqlHelper) {
		this.sqlHelper = sqlHelper;
	}

}
